package com.three.pinmore.pin.view.activity;

import android.os.Build;
import android.os.Handler;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Window;
import android.view.WindowManager;

import com.three.pinmore.R;
import com.three.pinmore.base.BaseActivity;
import com.three.pinmore.pin.view.fragment.HomeFragment;
import com.three.pinmore.pin.view.fragment.MyFragment;
import com.three.pinmore.pin.view.fragment.NewThingFragment;
import com.three.pinmore.pin.view.fragment.SearchFragment;
import com.three.pinmore.pin.view.fragment.ShoppingFragment;

import butterknife.BindView;
import etong.bottomnavigation.lib.BottomNavigationBar;

/**
 * Created by YWT love meng on 2017/8/30
 */
public class MainActivity extends BaseActivity {

    @BindView(R.id.bottomLayout)
    BottomNavigationBar bottomLayout;

    @Override
    public int getLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

        getSupportFragmentManager().beginTransaction().replace(R.id.container, HomeFragment.newInstance()).commitAllowingStateLoss();

        setUpBottomNavigationBar();
    }

    @Override
    public void initData() {

    }

    public void setUpBottomNavigationBar() {

        bottomLayout = (BottomNavigationBar) findViewById(R.id.bottomLayout);
        bottomLayout.addTab(R.drawable.selector_home, "home", 0xFF55738B);
        bottomLayout.addTab(R.drawable.selector_newthing, "newthing", 0xff096c54);
        bottomLayout.addTab(R.drawable.selector_shopping, "shopping", 0xFF701919);
        bottomLayout.addTab(R.drawable.selector_search, "search", R.color.firebrick);
        bottomLayout.addTab(R.drawable.selector_my, "my", R.color.butouming);
        bottomLayout.setOnTabListener(new BottomNavigationBar.TabListener() {
            @Override
            public void onSelected(int i) {
                Fragment fragment = null;
                switch (i) {
                    case 0:
                        fragment = HomeFragment.newInstance();
                        break;
                    case 1:
                        fragment = NewThingFragment.newInstance();
                        break;
                    case 2:
                        fragment = ShoppingFragment.newInstance();
                        break;
                    case 3:
                        fragment = SearchFragment.newInstance();
                        break;
                    case 4:
                        fragment = MyFragment.newInstance();
                        break;
                    default:
                        fragment = HomeFragment.newInstance();
                        break;
                }
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.container, fragment)
                        .commitAllowingStateLoss();
            }
        });
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                bottomLayout.requestLayout();
            }
        }, 1000);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}
